package org.zjsru.product.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjsru.product.pojo.Product;
import org.zjsru.product.service.ProductService;
import org.zjsru.product.service.UserFeignClient;

import java.util.List;

/**
 * 商品控制器
 */
@Slf4j
@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 根据ID查询商品详情
     */
    @GetMapping("/detail/{id}")
    public Product getProductDetail(@PathVariable Long id, @RequestParam(required = false) Long userId) {
        // 如果提供了用户ID，验证用户状态
        if (userId != null) {
            boolean userStatus = userFeignClient.checkUserStatus(userId);
            if (!userStatus) {
                log.warn("用户状态异常，用户ID: {}", userId);
                throw new RuntimeException("用户状态异常");
            }
        }
        
        log.info("查询商品详情，商品ID: {}, 用户ID: {}", id, userId);
        return productService.getProductById(id);
    }

    /**
     * 根据ID查询商品
     */
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Long id) {
        log.info("查询商品，ID: {}", id);
        return productService.getProductById(id);
    }

    /**
     * 查询所有商品
     */
    @GetMapping
    public List<Product> getAllProducts() {
        log.info("查询所有商品");
        return productService.getAllProducts();
    }

    /**
     * 根据分类查询商品
     */
    @GetMapping("/category/{category}")
    public List<Product> getProductsByCategory(@PathVariable String category) {
        log.info("查询分类商品，分类: {}", category);
        return productService.getProductsByCategory(category);
    }

    /**
     * 创建商品
     */
    @PostMapping
    public Product createProduct(@RequestBody Product product) {
        log.info("创建商品: {}", product.getName());
        return productService.createProduct(product);
    }

    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public Product updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        log.info("更新商品，ID: {}", id);
        return productService.updateProduct(product);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable Long id) {
        log.info("删除商品，ID: {}", id);
        return productService.deleteProduct(id);
    }

    /**
     * 扣减库存
     */
    @PostMapping("/{id}/stock/decrease")
    public boolean decreaseStock(@PathVariable Long id, @RequestParam Integer quantity) {
        log.info("扣减库存，商品ID: {}, 数量: {}", id, quantity);
        return productService.decreaseStock(id, quantity);
    }

    /**
     * 恢复库存
     */
    @PostMapping("/{id}/stock/increase")
    public boolean increaseStock(@PathVariable Long id, @RequestParam Integer quantity) {
        log.info("恢复库存，商品ID: {}, 数量: {}", id, quantity);
        return productService.increaseStock(id, quantity);
    }

    /**
     * 搜索商品
     */
    @GetMapping("/search")
    public List<Product> searchProducts(@RequestParam String name) {
        log.info("搜索商品，关键词: {}", name);
        return productService.searchProductsByName(name);
    }
} 